//  This is the sensor script for the Helixoide SlServ Device
//  It implements the Helixoide SlServ protocol for Version 1.5
//  Current Implementation is based on irc.helixoide.com (DarkDna irc network)
//  If you want to help Develop contact Ross D. at posiden@helixoide.com

//  Helixoide Solarian Sl-Status system: sensor.lsl
//  Based On The Helixoide SlServ Device 1.5 beta 2
//  Created by Ross Delinger (aka: Posiden Dryke)
//  Last Update: Sunday 28th of Feburary 2010 11:39AM EST
//  SlServ Requirments: Helixoide SLServ 1.5
//  SlServ Code can be Found on http://code.google.com

list peopleInRange = [];
list formattedNicks = [];
list peopleUUIDs = [];
integer noSendLimit = 50;
integer noSendCount;
integer sensorRepeat = 15;
integer urlUpdate = 10;//how many sensor sweeps to wait for before sending url data again
integer updateCount;
integer nullCount;//used to verify sensor data, there seems to be an issue with the sensor returning NULL when it shouldn't
integer findPeopleInList(string name)
{
    integer i = 0;
    integer index = -1;
    for(i=0;i<llGetListLength(peopleInRange);i++)
    {
        if(llList2String(peopleInRange,i)==name)
        {
            index = i;
        }
    }
    return index;
}
sendChatToIRC(string user, string chat)
{
    list tokens = [(string)llGetKey(),"PRIVMSG",user,chat];
    string theMsg = llDumpList2String(tokens,"&");
    llMessageLinked(LINK_SET,1337,theMsg,"MSG");
}    
string formatName(string name)
{
    list seperated = llParseString2List(name,[" "],[]);
    seperated = ["SL"] + seperated;
    string nick = llDumpList2String(seperated,"-");
    return nick;
    
}
string lastData;
sendUserCommand(list nicks, list uuids)
{
    //the users command is structured like this device&USERS&nick>uuid<nick>uuid
    if(updateCount==urlUpdate)
    {
        llMessageLinked(LINK_SET,1337,"0","init");
        updateCount = 1;
    }
    else if(updateCount < urlUpdate)
    {
        updateCount++;
    }
    if(llGetListLength(nicks)>0 && llGetListLength(uuids)>0)
    {
        list step1 = [];//step 1 formats the users into the nick>uuid format
        integer i;
        for(i = 0;i<llGetListLength(nicks);i++)
        {
            step1 += llDumpList2String([llList2String(nicks,i),llList2Key(uuids,i)],">");//format idividual users
        }
        string data = llDumpList2String(step1,"<");//formats the data into the < seperated format required
        string completeData = llDumpList2String([(string)llGetKey(),"USERS&"],"&") + data;//adds the proper prefixes
        if(completeData!=lastData)
        {
            llMessageLinked(LINK_SET,1337,completeData,"MSG");
            lastData = completeData;
        }
        else
        {
            if(noSendCount<50)
            {
                noSendCount++;
            }
            else
            {
                lastData = "";
                noSendCount = 0;
            }
        }
    }
    else
    {
        //we need to send the NULL command stating there are no users in range
        string completeData = llDumpList2String([(string)llGetKey(),"USERS&"],"&") + "NULL";//adds the proper prefixes
        if(nullCount>0)
        {
            if(completeData!=lastData)
            {
            
                llMessageLinked(LINK_SET,1337,completeData,"MSG");
                lastData = completeData;
            }
            else
            {
                if(noSendCount<50)
                {
                    noSendCount++;
                }
                else
                {
                    lastData = "";
                    noSendCount = 0;
                }
            }
            nullCount = 0;
        }
        else
        {
            nullCount++;
        }
    }
        
}
default
{
    state_entry()
    {
        
        
        
    }
    touch_start(integer numd)
    {
        lastData = "";
        llMessageLinked(LINK_SET,1337,"0","init");
        llSensorRepeat("","",AGENT,20,PI,sensorRepeat);
    }
    sensor(integer numd)
    {
        integer i = 0;
        list namesFoundInSensorSweep = [];
        list keysFoundInSensorSweep = [];
        list nicksFoundInSensorSweep = [];
        for(i = 0;i<=numd;i++)
        {
            
            if(llDetectedKey(i)!=NULL_KEY)
            {
                
                
                namesFoundInSensorSweep += llDetectedName(i);
                keysFoundInSensorSweep += llDetectedKey(i);
                nicksFoundInSensorSweep += formatName(llDetectedName(i));
            }
        }
        sendUserCommand(nicksFoundInSensorSweep,keysFoundInSensorSweep);
        peopleUUIDs = keysFoundInSensorSweep;
        peopleInRange = namesFoundInSensorSweep;
        formattedNicks = nicksFoundInSensorSweep;
    }
    no_sensor()
    {
        sendUserCommand([],[]);
        peopleUUIDs = [];
        peopleInRange = [];
        formattedNicks = [];
        
    }
            
}
